#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>


struct TreeNode
{
    int val;
    TreeNode*left;
    TreeNode*right;
};

void print_tree(TreeNode*root)
{
    if (!root)
    {
        return ;
    }

    std::queue<TreeNode*>q;
    q.push(root);

    while (!q.empty())
    {
        int n = q.size();
        while (n--)
        {
            TreeNode*node = q.front();
            q.pop();
            if (node->left)
                q.push(node->left);
            if (node->right)
                q.push(node->right);
            std::cout<<node->val<<" ";

        }
        std::cout<<"\n";
    }
}

TreeNode* tree_builder(std::vector<int>&v)
{
    // 创建根节点
    TreeNode*root = new TreeNode;
    root->val = v[0];
    root->left = nullptr;
    root->right = nullptr;

    // 因为是自己测试用，我就不判空了
    std::queue<TreeNode*>q;
    q.push(root);

    // 遍历数组并且添加节点
    int count = 1;
    int n = v.size();
    while (count <n&&!q.empty())
    {
        int sz = q.size();

        while (sz--)
        {
            TreeNode*temp = q.front();
            q.pop();

            if (count<n&&v[count]!=-1)
            {
                TreeNode*node = new TreeNode;
                node->val = v[count];
                node->left = nullptr;
                node->right = nullptr;
                q.push(node);
                temp->left = node;
            }
            count++;

            if (count<n&&v[count] != -1)
            {
                TreeNode*node = new TreeNode;
                node->val = v[count];
                node->left = nullptr;
                node->right = nullptr;
                q.push(node);
                temp->right = node ;
            }
            count ++;
        }
    }

    return root;
}

int main()
{
    std::vector<int>v = {5,4,8,1,11,-1,9,-1,-1,2,7};
    TreeNode*root = tree_builder(v);

    print_tree(root);
    return 0;
}